let wxToastLock = false;
let wxLoadingLock = false;
export function timeoutTask(task, time) {
  let tmpTimeout = setTimeout(() => {
    task();
    clearTimeout(tmpTimeout);
  }, time);
}
export function isNumber(val) {
  const regPos = /^\d+(\.\d+)?$/; //非负浮点数

  const regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数

  let flag = false;

  if (regPos.test(val) || regNeg.test(val)) {
    flag = true;
  }
  return flag;
}

export const showWxToast = (pTip) => {
  if (!wxToastLock && !wxLoadingLock) {
    wxToastLock = true;
    wx.showToast({
      title: pTip,
      icon: "none",
    });
    timeoutTask(() => {
      hideWxToast();
    }, 1500);
  } else if (!wxToastLock && wxLoadingLock) {
    hideWxLoading();
    showWxToast(pTip);
  } else if (wxToastLock && !wxLoadingLock) {
    hideWxToast();
    showWxToast(pTip);
  } else {

  }
};

export const showWxLoading = (pTip) => {
  if (!wxToastLock && !wxLoadingLock) {
    wxLoadingLock = true;
    wx.showLoading({
      title: pTip,
      mask: true,
    });
  } else if (!wxToastLock && wxLoadingLock) {
    hideWxLoading();
    showWxLoading(pTip);
  } else if (wxToastLock && !wxLoadingLock) {
    hideWxToast();
    showWxLoading(pTip);
  } else {

  }
};

export const hideWxLoading = () => {
  if (wxLoadingLock) {
    wx.hideLoading();
    wxLoadingLock = false;
  } else {

  }
};
const hideWxToast = () => {
  if (wxToastLock) {
    wx.hideToast();
    wxToastLock = false;
  } else {

  }
};

// 获取随机值  min<= num < max
export const getRandom = (min, max) =>
  Math.floor(Math.random() * (max - min)) + min;

// 获取当前时间戳
export function getCurrentTimestamp() {
  return new Date().getTime();
}

// 消息审核验证
export function validateCheckResult(checkStr) {
  if (!checkStr) {
    return false;
  }
  const checkObj = JSON.parse(checkStr);
  if (checkObj.result.suggest === 'pass') {
    return true;
  }
  return false;

}